package com.tcgroup.phone.base_core.widget;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.databinding.DataBindingUtil;

import com.tcgroup.phone.base_core.BR;
import com.tcgroup.phone.base_core.R;
import com.tcgroup.phone.base_core.databinding.CoreWidgetTextLinearlayoutBinding;
import com.tcgroup.phone.base_core.framework.mvvm.activity.ContainerMvvmActivity;
import com.tcgroup.phone.base_core.widget.model.SmartEditLinearLayoutModel;

/**
 * 作者: wangfei
 * 创建时期: 2020/4/21.
 * 包名: com.tcgroup.phone.base_core.widget
 * 类名: OSAndroidMvvm
 */
public class SmartTextLinearLayout extends LinearLayout {

    private CoreWidgetTextLinearlayoutBinding mBinding;
    private SmartEditLinearLayoutModel model;
    private SetOnClickListener mSetOnClickListener;

    public SmartTextLinearLayout(Context context) {
        super(context);
        initView();
    }

    public SmartTextLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public SmartTextLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public SmartTextLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public void setOnClickListener(SetOnClickListener setOnClickListener){
        mSetOnClickListener = setOnClickListener;
    }

    public void initView(){
        mBinding = DataBindingUtil.inflate(LayoutInflater.from(getContext()),R.layout.core_widget_text_linearlayout,this,true);
        mBinding.getRoot().setOnClickListener(v -> {
            if (mSetOnClickListener != null){
                mSetOnClickListener.onRootClick();
            }
        });
    }

    public void setLifecycleOwner(ContainerMvvmActivity activity){
        mBinding.setLifecycleOwner(activity);
    }

    public SmartEditLinearLayoutModel getModel() {
        return model;
    }

    public void setModel(SmartEditLinearLayoutModel model) {
        this.model = model;
        mBinding.setVariable(BR.model,getModel());
        mBinding.executePendingBindings();
    }

    public TextView getTitleView(){
         return mBinding.tvTitle;
    }

    public TextView getContentView(){
        return mBinding.etContent;
    }

    public interface SetOnClickListener{
        void onRootClick();
    }

}
